package com.myzl.quartz.task;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.date.TimeInterval;
import com.myzl.coal.service.IHistoryDataService;
import com.myzl.coal.service.IStatisticsService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author Administrator
 * @date 2022年10月25日14:45:52
 */
@Component("dataStatisticsTask")
@Slf4j(topic="admin-user")
@Data
public class DataStatisticsTask {

    private final IStatisticsService statisticsService;
    private final IHistoryDataService historyDataService;

    public void statistics(){
        statisticsService.statistics();
    }

    public void scaleDashboard(){
        statisticsService.scaleDashboard(201);
    }

    public void clearMongoData(){
        statisticsService.clearMongoData();
    }

    public void mysqlToMongo(Integer pageSize) throws Exception {
        TimeInterval timer = new TimeInterval();
        String startTime = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_MS_PATTERN);
        log.info("数据迁移开始，页数据大小为：{} ",pageSize);
        int i = 0;
        while (true) {
            boolean flag = historyDataService.copyMysqlToMongo(pageSize);
            i++;
            log.info("数据迁移，执行第 {} 次，当前时间为：{} ",i, LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_MS_PATTERN));
            if (!flag){
                break;
            }
        }
        String endTime = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_MS_PATTERN);
        log.info("数据迁移开始时间：{} ",startTime);
        log.info("数据迁移结束时间：{} ",endTime);
        log.info("数据迁移任务耗时：{} 毫秒",timer.interval());
    }
}
